import Vue from 'vue';
import VueRouter from 'vue-router';
import Login from '@/views/Login.vue';
import Flyer from '@/views/Flyer.vue';
import Device from '@/views/Device.vue';
import NewAirSecurity from "../views/NewAirSecurity"
import {getToken} from '@/helper/auth';

Vue.use(VueRouter);

const routes = [
	{
		path: '/',
		redirect: '/newAirSecurity',
	},
	{
		path: '/login',
		component: Login
	},
	{
		path: '/newAirSecurity',
		component: NewAirSecurity,
		children: [
			{
				path: '',
				component: Flyer
			},
			{
				path: 'flyer',
				component: Flyer
			},
			{
				path: 'device',
				component: Device
			},
		]
	},

];

const router = new VueRouter({
	routes
});
router.beforeEach((to, from, next) => {
	var token = getToken()
	if (!token) {
		if (to.path === '/login') {
			next()
		} else {
			next({path: '/login'})
		}
	} else {
		if (to.path === '/login') {
			next(false)
		} else {
			next()
		}
	}
})
export default router;
